<?php
/**
 * 管理员控制器
 * MemberController.php
 * ==============================================
 * 版权所有 2013-2014
 * ----------------------------------------------
 * 这不是一个自由软件，未经授权不许任何使用和传播。
 * ----------------------------------------------
 * @date: 2014-4-29 下午12:24:24
 * @author: huangdaohai
 * @email:huangdaohai.2007@163.com 
 * ==============================================
*/
class MemberController extends AdminController 
{
	//用户列表
	public function actionIndex(){
		$criteria=new CDbCriteria();
		if(!empty($_POST['username'])){
			$criteria->compare('t.username',$_POST['username'],true);
		}
		
		$criteria->order='t.uid desc';
		$count=AdminMember::model()->count($criteria);
    	$pages=new CPagination($count);
    	
		$pages->pageSize=$this->pagesize;
	    $pages->applyLimit($criteria);
	    $model = AdminMember::model()->findAll($criteria);
		$this->render('index',array('model'=>$model,'pages'=>$pages));
	}
	//创建用户
	public function actionCreate(){
		$model = new AdminMember('register');
		if(isset($_POST['ajax']) && $_POST['ajax']==='member-_form-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
		if(isset($_POST['AdminMember'])){
			$model->attributes=$_POST['AdminMember'];
			$model->salt=Yii::app()->common->randCode(5);
			if($model->save()){
				AdminLog::add('添加管理员'.$model->username);
				$alert="SendWebMsg('success','管理员添加成功','".$this->createUrl('member/index')."')";
			}else{
				if($model->hasErrors()){
					$alert="SendWebMsg('cue','请修正页面错误提示项')";
				}else{
					$alert="SendWebMsg('error','你的网络不给力，操作失败')";
				}
			}
			Yii::app()->getClientScript()->registerScript('msg',$alert,CClientScript::POS_END);
		}
        $this->render('create',array('model'=>$model));
	}
	//更新用户资料
	public function actionUpdate($id){
		$model = AdminMember::model()->findByPk($id);
		if(isset($_POST['AdminMember']['role_id']) && !empty($_POST['AdminMember']['role_id'])){
			$_POST['AdminMember']['role_id']=join(',', $_POST['AdminMember']['role_id']);
		}
		if(isset($_POST['ajax']) && $_POST['ajax']==='member-_form-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
		if(isset($_POST['AdminMember'])){
			$model->scenario='update';
			$model->attributes=$_POST['AdminMember'];
			if($model->save()){
				AdminLog::add('编辑管理员资料'.$model->username);
				$alert="SendWebMsg('success','管理员资料编辑成功','".$this->createUrl('member/index')."')";
			}else{
				if($model->hasErrors()){
					$alert="SendWebMsg('cue','请修正页面错误提示项')";
				}else{
					$alert="SendWebMsg('error','你的网络不给力，操作失败')";
				}
			}
			//echo '<pre>';print_r($model->errors);exit;
			Yii::app()->getClientScript()->registerScript('msg',$alert,CClientScript::POS_END);
		}
		if(!empty($model->role_id)){
			$user_role = explode('|', $model->role_id);
		}else{
			$user_role = array(0);
		}
		//查询所有的角色
		$model_roles = AdminRole::model()->findAll('status=1 and role_id!=1');
		$roles = array();
		foreach ($model_roles as $v){
			$roles[$v->role_id]=$v->role_name;
		}
		$this->render('update',array('model'=>$model,'roles'=>$roles,'user_role'=>$user_role));
	}
	//修改用户密码
	public function actionEditpass($id){
		$model = AdminMember::model()->findByPk($id);
		$model->scenario='editpass';
		if(isset($_POST['ajax']) && $_POST['ajax']==='member-_form-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
		if(isset($_POST['AdminMember'])){
			$model->attributes=$_POST['AdminMember'];
			if($model->save()){
				$this->redirect($this->createUrl('member/index'));
			}
		}
		$model->password='';
		$model->password2='';
		$this->render('editpass',array('model'=>$model));
	}
    //修改个人密码 updatepersonpass
	public function actionEditpersonpass(){
	    $uid = Yii::app()->user->id ? Yii::app()->user->id : 0;
		$model = AdminMember::model()->findByPk($uid);
		$model->scenario='editpersonpass';
		if(isset($_POST['ajax']) && $_POST['ajax']==='memberpass-_form-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
		if(isset($_POST['AdminMember'])){
		    //验证老密码是否正确
            $sOldPassword = md5( md5($_POST['oldpassword']) . $model->salt );
            if( $sOldPassword != $model->password ){
                $alert="SendWebMsg('error','旧密码不正确请重新输入')";
            } else {
                $model->attributes=$_POST['AdminMember'];
    			if($model->save()){
    				$alert="SendWebMsg('success','用户密码修改成功','".$this->createUrl('/admin/default/logout')."','editpersonpass')";
    			}else{
    				if($model->hasErrors()){
        				$alert="SendWebMsg('cue','请修正页面错误提示项')";
        			}else{
    	    			$alert="SendWebMsg('error','你的网络不给力，操作失败')";
        			}
    			}
            }
			
			Yii::app()->getClientScript()->registerScript('msg',$alert,CClientScript::POS_END);
		}
		$model->password='';
		$model->password2='';
		$this->render('editpersonpass',array('model'=>$model));
	}
    
	//删除用户
	public function actionDelete($id){
		$result=AdminMember::model()->findByPk($id,'uid!=1');
		if($result && $result->delete()){
/* 		    $oPower = AssignmentsModel::model()->find('userid=:userid',array(':userid'=>$id));
            if($oPower) $oPower->delete(); */
			$this->ajaxReturn(0,'删除成功');
		}else{
			$this->ajaxReturn(1,'删除失败');
		}
	}
	//批量删除用户
	public function actionDeleteall(){
		$check=1;
		$ids=explode('|',$_POST['ids']);
		foreach ((array)$ids as $v){
			$result=AdminMember::model()->findByPk($v,'uid!=1');
			if($result && $result->delete()){
				/* $oPower = AssignmentsModel::model()->find('userid=:userid' ,array(':userid'=>$v));
                if($oPower) $oPower->delete(); */
			}else{
				$check=0;
			}
		}
		if($check){
			$this->ajaxReturn(0,'删除成功');
		}else{
			$this->ajaxReturn(1,'删除失败');
		}
	}
}